
//import { Promise } from 'es6-promise'

import { Message, MessageBox } from 'element-ui'
const errorCodeHandle = (resp,reject,nullTip)=>{
    if(resp.code){
        if(resp.code == 204){
            setMsgTip(store,{
                text:resp.msg,
                type:'error'
            })
            reject(new Error(resp.msg))
            return true
        }
        else if(resp.code == 401 || resp.code == 23003 ){
            setMsgTip(store,{
                text:resp.msg,
                type:'error'
            })
            setNeedLogin(store, true)
            reject(new Error(resp.msg))
            return true
        }
        setMsgTip(store, {
            text:resp.msg,
            type:'warn'
        })
        reject(new Error(resp.msg))
        return true
    }
    if(nullTip){
        if(!resp.data){
            setMsgTip(store, {
                text:resp.msg,
                type:'warn'
            })
            reject(new Error(resp.msg))
            return
        }
    }
    return false
}
// errorRequestHandle
const errorRequestHandle = (text,reject)=>{
    Message({
        message: text,
        type: 'error',
        duration: 5 * 1000
      })
    reject(new Error(text));
}


const convertRequestURL = function(url,environment) {
    let converted_url
    
    let channel = process.env.CHANNEL
    // console.log(mode)
    converted_url="http://47.97.204.172:9026/"+url
    return converted_url
}

const loginAndOutApi = {
    
    statistics(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("loan/data/statistics"),
                data:JSON.stringify(params),
                dataType:"json",
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp.data
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求贷款超市数据失败',reject)
                }
            })
        })
    },
    exportFile(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("loan/data/export"),
                data:JSON.stringify(params),
                dataType:"json",
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp.data
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求导出失败',reject)
                }
            })
        })
    },
}

export {
    dashBoardApi,
}
export function getInfo(token) {
  return request({
    url: '/user/info',
    method: 'get',
    params: { token }
  })
}
//import request from '@/utils/request'

/*export function login(username, password) {
  return request({
    url: '/user/login',
    method: 'post',
    data: {
      username,
      password
    }
  })
}

export function getInfo(token) {
  return request({
    url: '/user/info',
    method: 'get',
    params: { token }
  })
}

export function logout() {
  return request({
    url: '/user/logout',
    method: 'post'
  })
}*/